#!/bin/bash
# source ./function_jsonstring.sh
:<<!
一些公共方法
!

# 将数组对象转成字符串且转义。只能有一个参数:数组的字符串(必填)
function getJsonStringWithEscapeFromArrayString() {
    local arrayArg
    arrayArg=(` echo "$@" `)

    #echo "arrayArg=${arrayArg[*]}"
    arrayLength=${#arrayArg[*]}
    #echo "arrayLength=${arrayLength}"
    if [ ${arrayLength} == 0 ]; then
        return 1
    fi


    if [ ${arrayLength} == 1 ]; then
        devBranceMap=${arrayArg[0]}
        #echo "------------------0:${devBranceMap}"
        arrayJsonResultString="["
        arrayJsonResultString+=${devBranceMap}
        arrayJsonResultString+="]"
    else
        for ((i=0;i<${arrayLength};i++))
        do
            devBranceMap=${arrayArg[i]}
            #echo "------------------${i}:${devBranceMap}"
            if [ ${i} == 0 ]; then
                arrayJsonResultString="["
            fi
            
            arrayJsonResultString+="\""  # 要转义
            arrayJsonResultString+=${devBranceMap}
            arrayJsonResultString+="\""  # 要转义
            
            if [ ${i} -lt $((arrayLength-1)) ]; then
                arrayJsonResultString+=","
            fi
            
            if [ ${i} == $((arrayLength-1)) ]; then
                arrayJsonResultString+="]"
            fi
        done
    fi
    
    echo "${arrayJsonResultString}" #这是函数的返回值，所以本方法中不要有多余的echo，且echo中不要有多余的东西
    return 0
}
